package SmallDataset;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Random;

import vo.User;
import JDBC.DatabaseDao;
import Utils.Constants;
import Utils.FileUtils;
import dao.UserDao;

public class Init {
	public static void randDataset() {

		// 随机算微博平均数
		ArrayList<String> userIds = UserDao.getAllIds();
		Random rand = new Random();
		ArrayList<Integer> buffer = new ArrayList<Integer>();
		String result = "";
		int i = 0;
		while (i < Constants.USER_RANDOM_SIZE) {
			int id = rand.nextInt(Constants.USER_SIZE);
			if (!buffer.contains(id)) {
				buffer.add(id);
				String userId = userIds.get(id);
				result += userId + "\n";
				i++;
			}
		}
		FileUtils.write(Constants.USERID_PATH, result);

	}

	public static ArrayList<String> loadUserIds() {
		// 从随机文件中load
		ArrayList<String> userIds = new ArrayList<String>();
		String fileContent = FileUtils.readFile(Constants.USERID_PATH, "utf-8");
		String lines[] = fileContent.split(Constants.LINE_SEPERATOR);
		for (String userId : lines) {
			userIds.add(userId);
		}
		return userIds;
	}

	public static ArrayList<User> loadUsers() {
		// 从随机文件中load
		ArrayList<User> users = new ArrayList<User>();
		String fileContent = FileUtils.readFile(Constants.USERID_PATH, "utf-8");
		String lines[] = fileContent.split(Constants.LINE_SEPERATOR);
		System.out.println("文件用户数:" + lines.length);
		String ids = "";
		int count = 0;
		for (String userId : lines) {
			ids += userId + ",";
		}

		ids = ids.substring(0, ids.length() - 1);
		String sql = "select user_id,followers_count,verified,statuses_count from users where user_id in(" + ids + ")";
		try {
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String userId = rs.getString("user_id");
				int followsCount = rs.getInt("followers_count");
				int verified = rs.getInt("verified");
				int statusesCount = rs.getInt("statuses_count");
				User user = new User();
				user.setUserId(userId);
				user.setFollowsCount(followsCount);
				user.setVerified(verified);
				user.setStatusesCount(statusesCount);
				users.add(user);
				count++;
			}
			System.out.println("多少ids:" + count);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return users;
	}

	public static String getCreatedTimeRange() {
		ArrayList<String> userIds = loadUserIds();
		String ids = "";
		for (String userId : userIds) {
			ids += userId + ",";
		}
		ids = ids.substring(0, ids.length() - 1);
		String result = "";

		try {
			String sql = "select min(created_at),max(created_at)  from statuses where user_id in (" + ids + ")";
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			rs.next();
			String startTime = rs.getString(1);
			String endTime = rs.getString(2);
			result += startTime.substring(0, startTime.length() - 2) + "@";
			result += endTime.substring(0, endTime.length() - 2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void upadteLengths() {
		String sql = "update statuses set length=len(content)";
		DatabaseDao.updateData(sql);
	}

	// 初始化时间-个数的hashtable，值设置为0
	public static Hashtable<String, Integer> initWeekCount(String startTime, int days) {
		Hashtable<String, Integer> week_count = new Hashtable<String, Integer>();
		// 存余数为key,表示从开始时间的第几周
		int totalWeek = days / 7;
		for (int k = 0; k < totalWeek; k++) {
			week_count.put(String.valueOf(k), 0);
		}
		return week_count;
	}

	public static void main(String args[]) {
		randDataset();
	}
}
